package cn.lichenfei.jfx.music.gui;

import cn.hutool.core.lang.Console;
import cn.lichenfei.jfx.music.Application;

import cn.lichenfei.jfx.music.common.model.AudioInfo;
import cn.lichenfei.jfx.music.common.utils.AudioDataUtil;
import cn.lichenfei.jfx.music.common.utils.JFUtils;
import javafx.animation.TranslateTransition;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Window;
import javafx.util.Duration;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsOutlined;
import org.kordamp.ikonli.javafx.FontIcon;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ChenFei
 * @date 2022/4/21
 */
public class MusicList extends StackPane {

    private TranslateTransition transition = new TranslateTransition(Duration.seconds(0.5), this);//移动动画

    {
        this.getStylesheets().addAll(Application.class.getResource("css/music-list.css").toExternalForm());
        this.setTranslateY(600);
    }

    private VBox container = new VBox();
    private HBox topBox = new HBox();
    private HBox topLeftBox = new HBox();
    private Label titleLabel = new Label("音频列表");
    private Label importLabel = new Label();
    private HBox topRightBox = new HBox();
    private Button hideBut = new Button();

    {
        this.getChildren().add(container);
        container.getChildren().add(topBox);
        topBox.getChildren().addAll(topLeftBox, topRightBox);
        topLeftBox.getChildren().addAll(titleLabel, importLabel);
        importLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.IMPORT));
        importLabel.setTooltip(JFUtils.getTooltip("导入音频"));
        topRightBox.getChildren().add(hideBut);
        topBox.setHgrow(topRightBox, Priority.ALWAYS);
        //
        hideBut.setGraphic(new FontIcon(AntDesignIconsOutlined.CLOSE));
        //class
        topBox.getStyleClass().add("top-box");
        topLeftBox.getStyleClass().add("top-left-box");
        topRightBox.getStyleClass().add("top-right-box");
        titleLabel.getStyleClass().add("title-label");
        importLabel.getStyleClass().add("import-label");
        hideBut.getStyleClass().add("hide-but");
        //隐藏
        hideBut.setOnMouseClicked(event -> hide());
        //导入选择文件夹
        importLabel.setOnMouseClicked(event -> {
            DirectoryChooser dc = new DirectoryChooser();
            dc.setTitle("选择文件夹");
            Window window = importLabel.getScene().getWindow();
            File file = dc.showDialog(window);
            if (file != null) {
                Console.log(file.getPath());
                try {
                    //获取文件夹下的音频文件
                    List<AudioInfo> audioList = AudioDataUtil.getAudioList(file.getPath());
                    //去重合并
                    List<AudioInfo> audioInfoList = AudioDataUtil.add(audioList);
                    //写入到文件
                    AudioDataUtil.createCsv(audioInfoList);
                    //初始化列表数据
                    changeItemList(1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private ScrollPane listContainer = new ScrollPane();
    private VBox listBox = new VBox();

    {
        container.getChildren().add(listContainer);
        container.setVgrow(listContainer, Priority.ALWAYS);
        listContainer.setContent(listBox);
        //
        listContainer.getStyleClass().add("list-container");
        listBox.getStyleClass().add("list-box");
    }

    private static int pageNum = 1;
    private static final int pageSize = 20;
    private HBox bottomBox = new HBox();
    private Pagination pagination = new Pagination();

    {
        container.getChildren().add(bottomBox);
        bottomBox.getChildren().add(pagination);
        bottomBox.setAlignment(Pos.CENTER);
        bottomBox.getStyleClass().add("bottom-box");
        pagination.getStyleClass().add("cf-pagination");
        //分页属性
        pagination.setCurrentPageIndex(pageNum - 1);
        //初始化第一页数据
        changeItemList(1);
        //页面变化
        pagination.currentPageIndexProperty()
                .addListener((observableValue, number, t1) -> changeItemList(t1.intValue() + 1));
    }

    /**
     * 设置总页数
     *
     * @param size
     */
    private void setPageCount(int size) {
        pagination.setPageCount(size % pageSize == 0 ? size / pageSize : (size / pageSize) + 1);
    }

    /**
     * 修改分页的数据
     */
    private void changeItemList(int pageNum) {
        setPageCount(AudioDataUtil.getSize());
        //创建一个组，MusicItem会用到
        ToggleGroup group = new ToggleGroup();
        //
        listBox.getChildren().clear();
        List<AudioInfo> page = AudioDataUtil.page(pageNum, pageSize);
        List<MusicItem> musicItems = new ArrayList<>();
        for (int i = 0; i < page.size(); i++) {
            AudioInfo info = page.get(i);
            MusicItem musicItem = new MusicItem(info.getName(), (pageNum - 1) * pageSize + i, group, this);
            musicItem.deleteListener(index -> {
                //从列表删除
                AudioDataUtil.removeAudio(index);
                //重新渲染数据
                changeItemList(pagination.getCurrentPageIndex() + 1);
            });
            musicItems.add(musicItem);
        }
        listBox.getChildren().addAll(musicItems);
    }

    /**
     * 显示
     */
    public void show() {
        //动画
        transition.setFromY(600);
        transition.setToY(100);
        transition.play();
    }

    /**
     * 隐藏
     */
    public void hide() {
        transition.setFromY(100);
        transition.setToY(600);
        transition.play();
    }
}
